CC		= g++
NCC		= /usr/local/cuda/bin/nvcc -ccbin g++ -arch=compute_11

SRCEXT	= .cpp
CUEXT	= .cu
HDREXT	= .h
OBJEXT	= .o

MODE	= CUDA
WD_SIZE	= 5

CFLAGS	= -Iinclude -I/usr/local/cuda/include -D_$(MODE) -D_WD_SIZE=$(WD_SIZE)
LDFLAGS	= -L/usr/local/cuda/lib64 -L/usr/lib/nvidia-current -lcudart -lcuda

SRCDIR	= src
INCDIR	= include
OBJDIR	= bin

PROGRAM = random

SRCS	= $(foreach d,$(SRCDIR),$(wildcard $(addprefix $(d)/*,$(SRCEXT))))
CUSRCS	= $(foreach d,$(SRCDIR),$(wildcard $(addprefix $(d)/*,$(CUEXT))))
HDRS	= $(foreach d,$(INCDIR),$(wildcard $(addprefix $(d)/*,$(HDREXT))))
OBJS	= $(addsuffix .o, $(addprefix $(OBJDIR)/, $(basename $(SRCS))))
CUOBJS	= $(addsuffix .o, $(addprefix $(OBJDIR)/, $(basename $(CUSRCS))))

.PHONY: all objs clean clean_all show exec .FORCE
.SUFFIXES:

all: $(PROGRAM)

objs: $(OBJS)

$(OBJDIR)/%$(OBJEXT): %$(SRCEXT) $(HDRS) .FORCE
	mkdir $(dir $@) -p
	$(CC) -c $< -o $@ $(CFLAGS) -std=c++0x
	
$(OBJDIR)/%$(OBJEXT): %$(CUEXT) $(HDRS) .FORCE
	mkdir $(dir $@) -p
	$(NCC) $(CFLAGS) -c $< -o $@ 
	
$(PROGRAM): $(OBJS) $(CUOBJS)
ifeq ($(MODE),CUDA)
	$(CC) $(OBJS) $(CUOBJS) -o $(OBJDIR)/gpu_$@_$(WD_SIZE) $(LDFLAGS)
	@echo Type ./$(OBJDIR)/gpu_$@_$(WD_SIZE) to execute the program.
else
	$(CC) $(OBJS) $(CUOBJS) -o $(OBJDIR)/$@_$(WD_SIZE) $(LDFLAGS)
	@echo Type ./$(OBJDIR)/$@_$(WD_SIZE) to execute the program.
endif
	
clean:
	rm -r $(OBJDIR)/$(SRCDIR)
	
clean_all:
	rm -r $(OBJDIR)

exec:
	./$(OBJDIR)/$(PROGRAM)
	
.FORCE:

show:
	@echo 'PROGRAM     :' $(PROGRAM)
	@echo 'SRCDIR      :' $(SRCDIR)
	@echo 'INCDIR      :' $(INCDIR)
	@echo 'OBJDIR      :' $(OBJDIR)
	@echo 'SRCS        :' $(SRCS)
	@echo 'CUSRCS      :' $(CUSRCS)
	@echo 'HDRS        :' $(HDRS)
	@echo 'OBJS        :' $(OBJS)
	@echo 'CUOBJS      :' $(CUOBJS)
	@echo 'CFLAGS      :' $(CFLAGS)
	@echo 'LDFLAGS     :' $(LDFLAGS)
	
